package top.fzqblog.firstapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import static android.os.Environment.getDownloadCacheDirectory;

/**
 * Created by 抽离 on 2017/9/12 0012.
 */

public class FileTestActivity extends Activity implements View.OnClickListener{

    private ImageView fileTestIV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filetestlayout);
        fileTestIV = findViewById(R.id.fileTestIV);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_filepath:
                File file = getFilesDir();
                Toast.makeText(this, "路径：" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_getAssets:
                try {
                    InputStream in = getAssets().open("三体智子.jpg");
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    fileTestIV.setImageBitmap(bitmap);
                    fileTestIV.setVisibility(View.VISIBLE);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_copyAssertsToData:
//                InputStream in = null;
//                FileOutputStream fos = null;
                try {
//                   in = getAssets().open("三体智子.jpg");
//                   fos = openFileOutput("santi.jpg", MODE_PRIVATE);
//                    byte[] bytes = new byte[in.available()];
//                    in.read(bytes);
//                    fos.write(bytes);
                    FileOutputStream outStream = openFileOutput("itcast.txt", Context.MODE_PRIVATE);
                    outStream.write("测试".getBytes());
                    outStream.flush();
                    outStream.close();
                    InputStreamReader inputStreamReader = new InputStreamReader(openFileInput("itcast.txt"));
                    char[] c = new char[1024];
                    inputStreamReader.read(c);
                    Toast.makeText(this, "内容：" + new String(c), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_copyImgToData:
                try {
                    FileOutputStream fos = openFileOutput("star.jpg", Context.MODE_PRIVATE);
                    InputStream in = getAssets().open("star.jpg");
                    byte[] bytes = new byte[in.available()];
                    in.read(bytes);
                    fos.write(bytes);
                    fos.flush();
                    Toast.makeText(this, "文件路径：", Toast.LENGTH_SHORT).show();
                    in.close();
                    fos.close();
                    FileInputStream fis = openFileInput("star.jpg");
                    Bitmap bitmap = BitmapFactory.decodeStream(fis);
                    fileTestIV.setImageBitmap(bitmap);
                    fis.close();
                }  catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_copyImgToRoot:
                try {
                    File f = new File("/ball.jpg");
                    if(!f.exists()){
                        f.createNewFile();
                    }
                    FileOutputStream fos = new FileOutputStream(f);
                    InputStream in = getAssets().open("balloon.jpg");
                    byte[] bytes = new byte[in.available()];
                    in.read(bytes);
                    fos.write(bytes);
                    fos.flush();
                    in.close();
                    fos.close();
                    Toast.makeText(this, f.getAbsolutePath()+"写完成", Toast.LENGTH_SHORT).show();
                }  catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_listFiles:
                String[] files = fileList();
                for(String f : files){
                        Log.e("tag", "file is "+ f);
                }
                break;
            case R.id.btn_getExternalFilesDir:
                Toast.makeText(this, "getExternalFilesDir：" + getExternalFilesDir("star.jpg"), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_getDownloadCacheDirectory:
                Toast.makeText(this, "getDownloadCacheDirectory.getAbsolutePath()：" +
                        getDownloadCacheDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_getDataDirectory:
                Toast.makeText(this, "getDataDirectory().getAbsolutePath():" +
                        Environment.getDataDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_getExternalStorageDirectory:
                Toast.makeText(this, "Environment.getExternalStorageDirectory().getAbsolutePath()：" +
                        Environment.getExternalStorageDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();
                break;
            case  R.id.btn_isExternalStorage:
                Toast.makeText(this, "是否插入内存卡：" +
                        Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_writeImgToExternalStorage:
                File f = Environment.getExternalStorageDirectory();
                File newFile = new File(f, "智子.jpg");
                if(!newFile.exists()){
                    try {
                        newFile.createNewFile();
                        InputStream in = getAssets().open("三体智子.jpg");
                        byte[] bytes = new byte[in.available()];
                        FileOutputStream fos = new FileOutputStream(newFile);
                        in.read(bytes);
                        fos.write(bytes);
                        fos.flush();
                        in.close();
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        }
    }
}
